package com.auth.manager.web.sys;

import com.auth.manager.common.resp.Result;
import com.auth.manager.pojo.sys.RoleMenuRelation;
import com.auth.manager.pojo.sys.SysRole;
import com.auth.manager.service.sys.RoleMenuRelationService;
import com.auth.manager.service.sys.SysRoleService;
import com.auth.manager.utils.page.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:55
 * @Author ZhuYouBin
 * @Description 系统角色
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private RoleMenuRelationService roleMenuRelationService;

    @PostMapping("/create")
    public Result create(@RequestBody @Valid SysRole sysRole) {
        sysRoleService.createRole(sysRole);
        return Result.success("新增成功");
    }

    @GetMapping("/delete/{rid}")
    public Result remove(@PathVariable String rid) {
        sysRoleService.removeRole(rid);
        return Result.success("删除成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Valid SysRole sysRole) {
        sysRoleService.updateRole(sysRole);
        return Result.success("更新成功");
    }

    @GetMapping("/queryPage")
    public Result queryPage(SysRole sysRole, PageVo pageVo) {
        PageVo queryPage = sysRoleService.queryPage(sysRole, pageVo);
        return Result.success("查询成功").setData(queryPage);
    }

    @GetMapping("/list")
    public Result queryAllRole() {
        List<SysRole> list = sysRoleService.listAllRole();
        return Result.success("查询成功").setData(list);
    }

    @PostMapping("/assignMenuToRole")
    public Result assignMenuToRole(@RequestBody List<RoleMenuRelation> list) {
        roleMenuRelationService.assignMenuToRole(list);
        return Result.success("分配权限成功");
    }

    @GetMapping("/getUserRole/{uid}")
    public Result getUserRole(@PathVariable String uid) {
        List<SysRole> list = sysRoleService.queryUserRole(uid);
        return Result.success("查询成功").setData(list);
    }

}
